﻿using Abp.Domain.Entities.Auditing;
using Abp.Domain.Repositories;
using Camc.Quality.Dto.QualityPlatformDto;
using Camc.Quality.OperationHistory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace Camc.Quality
{
    public static class StaticOperation
    {
        public static PQualitWorkStepDto pQualitWorkStepDto { get; set; }
        /// <summary>
        /// 解压缩一个 zip 文件。
        /// </summary>
        /// <param name="zipedFile">The ziped file.</param>
        /// <param name="strDirectory">The STR directory.</param>
        /// <param name="password">zip 文件的密码。</param>
        /// <param name="overWrite">是否覆盖已存在的文件。</param>
        public static void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
        {
            if (strDirectory == "")
                strDirectory = Directory.GetCurrentDirectory();
            if (!strDirectory.EndsWith("\\"))
                strDirectory = strDirectory + "\\";
            using (ICSharpCode.SharpZipLib.Zip.ZipInputStream s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(zipedFile)))
            {
                s.Password = password;
                ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry;

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = "";
                    string pathToZip = "";
                    pathToZip = theEntry.Name;

                    if (pathToZip != "")
                        directoryName = Path.GetDirectoryName(pathToZip) + "\\";

                    string fileName = Path.GetFileName(pathToZip);

                    Directory.CreateDirectory(strDirectory + directoryName);

                    if (fileName != "")
                    {
                        if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
                        {
                            using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
                            {
                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);

                                    if (size > 0)
                                        streamWriter.Write(data, 0, size);
                                    else
                                        break;
                                }
                                streamWriter.Close();
                            }
                        }
                    }
                }

                s.Close();
            }
        }

        /// <summary>
        /// 对比字符串
        /// </summary>
        /// <param name="motherString"></param>
        /// <param name="childString"></param>
        /// <returns></returns>
        public static string TellingDifferencesFrom(this string motherString,string childString) 
        {
            var result = new StringBuilder();
            foreach(var c in motherString) 
            {
                if (!childString.Contains(c)) 
                {
                    result.Append(c);
                }
            }
            return result.ToString();
        }


        /// <summary>
        /// 清除字符串内空格
        /// </summary>
        /// <param name="str">需要处理的字符串</param>
        /// <returns>处理好后的字符串</returns>
        public static string Exceptblanks(this string str)
        {
            int _length = str.Length;
            if (_length > 0)
            {
                StringBuilder _builder = new StringBuilder(_length);
                for (int i = 0; i < str.Length; i++)
                {
                    char _c = str[i];
                    //switch (_c)
                    //{
                    //    case '\r':
                    //    case '\n':
                    //    case '\t':
                    //    case ' ':
                    //        continue;
                    //    default:
                    //        _builder.append(_c);
                    //        break;
                    //}
                    if (!char.IsWhiteSpace(_c))
                        _builder.Append(_c);
                }
                return _builder.ToString();
            }
            return str;
        }

        /// <summary>
        /// 去除字符串内的标签
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string SplitHtml2(this string Htmlstring)
        {
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<[^>]*>", "");
            return Htmlstring.Trim();
        }
    }
}
